#include"../logs/lyzlog.h"

void test_logger(const std::string& name)
{
    INFO("%s","测试开始！！！！");

    lyzlog::Logger::ptr logger = lyzlog::LoggerManager::getInstance().getLogger(name);
    logger->debug("%s", "测试日志");
    logger->info("%s", "测试日志");
    logger->warn("%s", "测试日志");
    logger->error("%s", "测试日志");
    logger->fatal("%s", "测试日志");

    INFO("%s","测试完成！！！！");

}

int main()
{ 
    std::unique_ptr<lyzlog::LoggerBuilder> builder(new lyzlog::GlobalLoggerBuilder);
    builder->BuilderFormatter("[%c][%f:%l][%p]%m%n");
    builder->BuilderLogName("lyz-Asynclog");
    builder->BuilderType(lyzlog::LoggerType::Sync_log);
    builder->BuilderLimitLev(lyzlog::Loglevel::value::DEBUG);
    builder->BuilderSink<lyzlog::StdoutSink>();
    builder->BuilderSink<lyzlog::FileSink>("./logfile/Asyncfile.log");
    builder->BuilderSink<lyzlog::RollSink>("./logfile/Roll-Asyn-By-Size.log", 1024 * 1024);
    builder->Build();
    test_logger("lyz-Asynclog");


    // std::string logger_name = "synclog";
    // lyzlog::Formatter::ptr fmt(new lyzlog::Formatter("[%d{%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n"));
    // lyzlog::Loglevel::value limit = lyzlog::Loglevel::value::INFO;

    // lyzlog::LogSink::ptr stdout_lsp = lyzlog::SinkFactory::create<lyzlog::StdoutSink>();
    // lyzlog::LogSink::ptr file_lsp = lyzlog::SinkFactory::create<lyzlog::FileSink>("./logfile/file.log");
    // lyzlog::LogSink::ptr rollfile_lsp = lyzlog::SinkFactory::create<lyzlog::RollSink>("./logfile/file.log", 1024 * 1024);
    // std::vector<lyzlog::LogSink::ptr> sinks{stdout_lsp, file_lsp, rollfile_lsp};

    // lyzlog::Logger::ptr logger(new lyzlog::SyncLogger(logger_name, limit, fmt, sinks));

    // -------测试缓冲区------
    // 读取文件数据，一点点写入缓冲区，最终将缓冲区数据写入文件，判断写入数据和源文件数据是否相同

    // std::ifstream ifs("./logfile/file.log", std::ios::binary);
    // if (ifs.is_open() == false)
    // {
    //     return -1;
    // }
    // ifs.seekg(0, std::ios::end); // 跳转到文件结尾
    // size_t fsize = ifs.tellg();  // 计算文件大小
    // ifs.seekg(0, std::ios::beg); // 跳转到文件开头
    // std::string body;
    // body.resize(fsize);
    // ifs.read(&body[0], fsize);
    // if (ifs.good() == false)
    // {
    //     std::cout << "read failed";
    //     return -1;
    // }
    // ifs.close();

    // lyzlog::Buffer buffer;
    // for(int i =0;i < body.size();i++)
    // {
    //     buffer.push(&body[i],1);
    // }
    // std::ofstream ofs("./logfile/tmp.log", std::ios::binary);
    // size_t rsize = buffer.readAbleSize();
    // for (int i = 0; i < rsize; i++)
    // {
    //     ofs.write(buffer.begin(),1);
    //     if(ofs.good() == false) {std::cout << "write error"; return -1;}
    //     buffer.moveRead(1);
    // }
    // ofs.close();



    // lyzlog::LogMsg log(lyzlog::Loglevel::value::INFO,"file.txt",15,"socket error","lyz7");
    // lyzlog::Formatter fmt;
    // std::string tmp = fmt.format(log);
    // lyzlog::LogSink::ptr stdout_lsp = lyzlog::SinkFactory::create<lyzlog::StdoutSink>();
    // lyzlog::LogSink::ptr file_lsp = lyzlog::SinkFactory::create<lyzlog::FileSink>("./logfile/file.log");
    // lyzlog::LogSink::ptr rollfile_lsp = lyzlog::SinkFactory::create<lyzlog::RollSink>("./logfile/file.log",1024*1024);
    // RollByTimeSink rollfiletime_lsp("./logfile/file.log",TimeGap::GAP_SECOND);
    // stdout_lsp->log(tmp.c_str(),tmp.size());
    // file_lsp->log(tmp.c_str(),tmp.size());
    // size_t cursize = 0;
    // size_t count = 0;
    // while(cursize < 1024*1024*10)
    // {
    //     std::string str = tmp + std::to_string(count++);
    //     rollfile_lsp->log(str.c_str(),str.size());
    //     cursize += str.size();
    // }
    // rollfiletime_lsp.log(tmp.c_str(),tmp.size());
    //  std::cout << lyzlog::util::Date::now() << std::endl;
    //  std::string pathname = "./abc/cde/a.txt";
    //  lyzlog::util::File::createDirectory(lyzlog::util::File::path(pathname));
    return 0;
}